﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using wp8nop.Model;

namespace wp8nop.ViewModel
{
    public class CarrelloViewModel : ViewModelBase
    {
        public event EventHandler<ProdottoViewModel> OnOpenDettaglioProdotto;
   

        public readonly IDataService _dataService;
        public CarrelloViewModel(IDataService data)
        {
            _dataService = data;

            Messenger.Default.Register<NotificationMessage>(this, NotificationMessageReceived);
        }


        private void NotificationMessageReceived(NotificationMessage msg)
        {
            Debug.WriteLine("Messaggio ricevuto: " + msg.Notification);
            if (msg.Notification == "UpdatedCarrello")
            {
                UpdateTotaleCarrello();
                
            }
        }

        private void UpdateTotaleCarrello()
        {
            var tot = _dataService.GetTotaleCarrello();
            TotaleCarrello = string.Format("Totale: {0:F2}", tot);

            if( tot == 0 )
                Spedizioni = "";
            else
            {
            var sped = _dataService.GetSpedizioni();
            if (sped == 0)
                Spedizioni = "Spedizione gratuita";
            else
                Spedizioni = string.Format("Spedizioni: {0:F2}", sped);
            }
        }

        internal void OpenDettaglioProdotto(ProdottoViewModel prod)
        {
            if (OnOpenDettaglioProdotto != null)
                OnOpenDettaglioProdotto(this, prod);
        }


        /// <summary>
        /// The <see cref="Spedizioni" /> property's name.
        /// </summary>
        public const string SpedizioniPropertyName = "Spedizioni";

        private string _spedizioni = "";

        /// <summary>
        /// Sets and gets the Spedizioni property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string Spedizioni
        {
            get
            {
                return _spedizioni;
            }

            set
            {
                if (_spedizioni == value)
                {
                    return;
                }

                RaisePropertyChanging(SpedizioniPropertyName);
                _spedizioni = value;
                RaisePropertyChanged(SpedizioniPropertyName);
            }
        }
        /// <summary>
        /// The <see cref="TotaleCarrello" /> property's name.
        /// </summary>
        public const string TotaleCarrelloPropertyName = "TotaleCarrello";

        private string _totaleCarrello = "";

        /// <summary>
        /// Sets and gets the TotaleCarrello property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string TotaleCarrello
        {
            get
            {
                return _totaleCarrello;
            }

            set
            {
                if (_totaleCarrello == value)
                {
                    return;
                }

                RaisePropertyChanging(TotaleCarrelloPropertyName);
                _totaleCarrello = value;
                RaisePropertyChanged(TotaleCarrelloPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="IsLoading" /> property's name.
        /// </summary>
        public const string IsLoadingPropertyName = "IsLoading";

        private bool _isLoading = false;

        /// <summary>
        /// Sets and gets the IsLoading property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool IsLoading
        {
            get
            {
                return _isLoading;
            }

            set
            {
                if (_isLoading == value)
                {
                    return;
                }

                RaisePropertyChanging(IsLoadingPropertyName);
                _isLoading = value;
                RaisePropertyChanged(IsLoadingPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="VisibilityLoading" /> property's name.
        /// </summary>
        public const string VisibilityLoadingPropertyName = "VisibilityLoading";

        private Visibility _visibilityLoading = Visibility.Collapsed;

        /// <summary>
        /// Sets and gets the VisibilityLoading property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public Visibility VisibilityLoading
        {
            get
            {
                return _visibilityLoading;
            }

            set
            {
                if (_visibilityLoading == value)
                {
                    return;
                }

                RaisePropertyChanging(VisibilityLoadingPropertyName);
                _visibilityLoading = value;
                RaisePropertyChanged(VisibilityLoadingPropertyName);
            }
        }
        /// <summary>
        /// The <see cref="SelectedProduct" /> property's name.
        /// </summary>
        public const string SelectedProductPropertyName = "SelectedProduct";

        private ProdottoViewModel _selectedProduct = null;

        /// <summary>
        /// Sets and gets the SelectedProduct property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ProdottoViewModel SelectedProduct
        {
            get
            {
                return _selectedProduct;
            }

            set
            {
                if (_selectedProduct == value)
                {
                    return;
                }

                RaisePropertyChanging(SelectedProductPropertyName);
                _selectedProduct = value;
                RaisePropertyChanged(SelectedProductPropertyName);
                if (SelectedProduct != null)
                    OpenDettaglioProdotto(SelectedProduct);
                
            }
        }
        public Categoria SelectedCategory { get; set; }
        /// <summary>
        /// The <see cref="Prodotti" /> property's name.
        /// </summary>
        public const string ProdottiPropertyName = "Prodotti";

        /// <summary>
        /// Sets and gets the Prodotti property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<ProdottoViewModel> Prodotti
        {
            get
            {
                UpdateTotaleCarrello();
                return _dataService.Carrello;
            }

        }

        public void LoadProducts()
        {
            try
            {
                
            }
            catch (System.Exception ex)
            {

                Debug.WriteLine("Eccezione " + ex.Message);
            }
        }


        internal void EliminaProdotto(ProdottoViewModel prod)
        {
            Prodotti.Remove(prod);
            Messenger.Default.Send(new NotificationMessage("UpdatedCarrello"));
        }

        internal void AumentaQuantita(ProdottoViewModel prod)
        {
            prod.Quantita++;
            Messenger.Default.Send(new NotificationMessage("UpdatedCarrello"));
        }

        internal void DiminuisciQuantita(ProdottoViewModel prod)
        {
            if (prod.Quantita > 1)
            {
                prod.Quantita--;
                Messenger.Default.Send(new NotificationMessage("UpdatedCarrello"));
            }

        }
    }
}
